

# 函数的嵌套
def counter(first=0):
    cnt = [first]

    def add_one():
        cnt[0] += 1
        return cnt[0]

    # 表示函数的引用
    return add_one


# num0()表示函数的调用add_one
num0 = counter()
print(num0())       #   1
print(num0())       #   2

num5 = counter(5)
print(num5())       #   6
print(num5())       #   7

num15 = counter(15)
print(num15())      #   16
print(num15())      #   17


# 普通函数的实现，一次传递三个参数
def line_0(a, b, x):
    return a * x + b


a = 2
b = 3
print("function common implementation ---- ")
print(line_0(a, b, 5))   # 13
print(line_0(a, b, 10))  # 23


# 闭包的实现，把三个变量分解为两次调用，一次两个变量，一次一个变量
def line_1(a, b):
    def arg_y(x):
        return a * x + b
    return arg_y
    # return lambda x: a * x + b


x = line_1(a, b)
print("function closure implementation --- ")
print(x(5))              # 13
print(x(10))             # 23
